package a日常研究._01网络通信练习;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket socket = new ServerSocket(7777);
        Socket clientSocket = socket.accept();
        System.out.println("客户端已连接");
        InputStream bis = clientSocket.getInputStream();
        int len;
        byte[] buf = new byte[1024];
        StringBuilder builder = new StringBuilder();
        while ((len = bis.read(buf))!= -1) {
            builder.append(new String(buf, 0, len));
        }
        System.out.println("接收到客户端传来的消息：" + builder);
        //回复客户端
        OutputStream bos = clientSocket.getOutputStream();
        bos.write("我收到了你的消息".getBytes());
        bos.flush();
        bis.close();
        bos.close();
        clientSocket.close();
    }
}
